package com.example.farmer.Fragment.Home;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

//转换买卖家

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.farmer.Adapter.Class.News_PostClassAdapter;
import com.example.farmer.Adapter.ClassAdapter;
import com.example.farmer.Database.ClassPostDatabase;
import com.example.farmer.Entity.ClassPost;
import com.example.farmer.Seller_Home.ActivityMyGoods;
import com.example.farmer.Activity.MainActivity;
import com.example.farmer.R;
import com.example.farmer.Seller_Home.ActivityPurchase;
import com.example.farmer.Setting.ServiceActivity;
import com.example.farmer.wode.AllordersActivity;

import java.util.List;

public class Seller_HomeFragment extends Fragment implements View.OnClickListener{
    private Button btnSellerHome;
    private View rootview;
    private ImageButton myGoodsBtn,order,purchase,evalue;
    private RecyclerView postrecyclerView;
    private TextView notNews;
    private ClassPostDatabase classPostDatabase;
    private List<ClassPost> classPosts;
    private ClassAdapter classAdapter;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
//    FragmentSellerhomeBinding binding;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        rootview = inflater.inflate(R.layout.fragment_sellerhome, container, false);


        myGoodsBtn = rootview.findViewById(R.id.my_goods_btn);
        order = rootview.findViewById(R.id.order);
        evalue = rootview.findViewById(R.id.evalue);
        purchase = rootview.findViewById(R.id.purchase);
        notNews = rootview.findViewById(R.id.not_news);


        // 初始化数据库并获取所有帖子
        classPostDatabase = ClassPostDatabase.getInstance(getContext());
        classPosts = classPostDatabase.getAllClassPosts();
        // 这里可以设置Adapter来显示列表数据
        postrecyclerView = rootview.findViewById(R.id.recyclerView_post);
        postrecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        //热点新闻
        classAdapter = new ClassAdapter(classPosts, getContext());
        postrecyclerView.setAdapter(classAdapter);



        updateUiWithPosts();

        //设置点击我的货品跳转
        myGoodsBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),ActivityMyGoods.class);
                startActivity(intent);
            }
        });

        //设置点击采购管理跳转
        purchase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ActivityPurchase.class);
                startActivity(intent);
            }
        });
        //设置点击订单管理跳转
        order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), AllordersActivity.class);
                startActivity(intent);
            }
        });
//设置点击客服管理跳转
        evalue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ServiceActivity.class);
                startActivity(intent);
            }
        });



// 买家首页转回买家首页
        btnSellerHome = rootview.findViewById(R.id.btnSellerHome);

        btnSellerHome.setOnClickListener(this);
        // 设置按钮的点击事件监听器
        return rootview ;
    }

    private void updateUiWithPosts() {
        // 如果posts不为空，则通知适配器数据已更改
        if (classPosts != null && !classPosts.isEmpty()) {
            // 假设PostAdapter实现了相应的方法来更新数据
            classAdapter = (ClassAdapter) postrecyclerView.getAdapter();
            if (classAdapter != null) {
                classAdapter.updatePosts(classPosts);
            }
        }
        else {
            notNews.setText("没有最新内容哦"); // 假设您有一个TextView来显示消息
        }
    }
    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.searchButton){

        } else if (v.getId() == R.id.btnSellerHome) {
            Intent intent = new Intent(getActivity() , MainActivity.class);
            startActivity(intent);
            getActivity().finish();
        }
    }
}